package com.hgk.common.common.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.hgk.appllication.MyApplication;

public class BallSharedPreferences {
	private static final String TAG = BallSharedPreferences.class.getSimpleName();
	private static final String PREFERENCE_NAME = "config";
	private SharedPreferences mPref;
    private BallSharedPreferences() {
    	if (MyApplication.getContext() != null) {
    		mPref = MyApplication.getContext().getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    	}
    }
    
    private static class PreferenceHolder {
    	private static BallSharedPreferences sInstance = new BallSharedPreferences();
    }
    
    public static BallSharedPreferences getInstance() {
    	return PreferenceHolder.sInstance;
    }

    public void saveStringConfig(String key, String value) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "saveStringConfig sInstance or mPref is null");
    		return;
    	}
        Editor editor = mPref.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void saveIntConfig(String key, int value) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "saveIntConfig sInstance or mPref is null");
    		return;
    	}
        Editor editor = mPref.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void saveBooleanConfig(String key, Boolean value) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "saveBooleanConfig sInstance or mPref is null");
    		return;
    	}
        Editor editor = mPref.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public String getStringConfig(String key, String defValue) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "getStringConfig with defalut sInstance or mPref is null");
    		return "";
    	}
        return mPref.getString(key, defValue);
    }
    
    public String getStringConfig(String key) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "getStringConfig sInstance or mPref is null");
    		return "";
    	}
        return mPref.getString(key, "");
    }

    public int getIntConfig(String key, int defValue) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "getIntConfig sInstance or mPref is null");
    		return 3;
    	}
        return mPref.getInt(key, defValue);
    }

    public Boolean getBooleanConfig(String key, boolean defValue) {
    	if (PreferenceHolder.sInstance == null || mPref == null) {
    		Log.e(TAG, "getBooleanConfig sInstance or mPref is null");
    		return false;
    	}
        return mPref.getBoolean(key, defValue);
    }
}